import logging.config
import os
import os.path

from pydantic import BaseModel
from loguru import logger
from pymongo import MongoClient
from redis.client import StrictRedis

project_path = os.path.dirname(__file__)


class Settings(BaseModel):
    basic: dict
    mongo: dict
    es: dict
    celery: dict
    log_config: dict
    dsn: str = "mongodb://admin:kejia@42.192.110.104:27017/?authMechanism=DEFAULT"
    redis: str = "redis://:kejia@42.192.110.104:6379"
    dsl: str = "http://elastic:kekejia@42.192.110.104:9200"

    class Config:
        env_file = ".env"


def init_settings():
    config_file = os.environ.get('CONFIG_FILE', os.path.join(project_path, 'temp_config.json'))
    with open(config_file, mode='r', encoding='utf-8') as f:
        content = f.read()
        # logger.info(content)
        # replace = project_path.replace("\\", "\\\\")
        content = content.replace('{{project_path}}', str(config_file))
    return Settings.parse_raw(content)


settings = init_settings()
aredis = StrictRedis.from_url(settings.redis, decode_responses=True)

client = MongoClient(settings.dsn, connect=False)
col_inst = client["kexie"]["institution"]
col_activity = client["kexie"]["activity"]
col_institution = client["kexie"]["institution"]
col_activity_institution = client["kexie"]["activity_institution"]
col_register = client["kexie"]["activity_register"]
col_alumni = client["kexie"]["alumni"]
col_okr = client["kexie"]["okr"]  # 年度目标考核
col_top11 = client["kexie"]["top11"] # 排行表
col_top11e = client["kexie"]["top11e"]  # 增强版排行表


col_institution_top11 = client["kexie"]["institution_top11"] # 排行表
col_institution_top11e = client["kexie"]["institution_top11e"]  # 增强版排行表
